package com.hnkywl.sts.dao.bg;

import com.hnkywl.sts.webservice.bg.request.BgWzbm;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 查询保供物资编码的使用状态
 * User: ccr
 * Date: 13-5-25
 * Time: 下午2:42
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class BgWzbmDao extends SimpleHibernateTemplate<BgWzbm,Long>{
    /**
     * 判断某个物资在系统中是否被使用
     * @param ggId
     * @return
     */
    public boolean existGg(Long ggId){
        String sql = "select sum(a) from (" +
                "select count(1) a from t_biz_cgdmx where gg_id = :ggId union all " +
                "select count(1) a from t_biz_cgjsdmx where gg_id = :ggId union all " +
                "select count(1) a from T_BIZ_HTMX where wz_id = :ggId union all " +
                "select count(1) a from T_BIZ_BGJHHZMX where wzgg_id = :ggId union all " +
                "select count(1) a from T_BIZ_BGJHMX where wzgg_id = :ggId union all " +
                "select count(1) a from T_BIZ_TPWZJHMX where wzgg_id = :ggId union all " +
                "select count(1) a from T_BIZ_ZGWZJHMX where wzgg_id = :ggId union all " +
                "select count(1) a from T_BIZ_KC where gg_id = :ggId union all " +
                "select count(1) a from T_BIZ_KH where JTWZ_ID = :ggId union all " +
                "select count(1) a from T_BIZ_DHD where wz_id = :ggId )";
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);
        sqlQuery.setLong("ggId",ggId);
        return Integer.parseInt(sqlQuery.uniqueResult().toString()) != 0;
    }

    public List<BgWzbm> findAll(){
        String query = "from BgWzbm where zt = 1 order by grade asc,code asc, czsj asc";
        return find(query);
    }
}
